package tts.controller.users;

import java.util.ArrayList;
import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;

import tts.controller.RedirectAction;
import tts.dao.UsersHome;
import tts.model.Users;

public class List extends RedirectAction {

	//fields
	private static final long serialVersionUID = -86966939953569102L;
	@Autowired
	private UsersHome usersHome;
	private Collection<Users> usersList;
	private Collection<Users> usersListDisabled;
	private ArrayList<Users> usersNotGroupedList=new ArrayList<Users>();

	//get & set
	public Collection<Users> getUsersList() {
		return usersList;
	}
	public void setUsersList(Collection<Users> usersList) {
		this.usersList = usersList;
	}
	public Collection<Users> getUsersListDisabled() {
		return usersListDisabled;
	}
	public void setUsersListDisabled(Collection<Users> usersListDisabled) {
		this.usersListDisabled = usersListDisabled;
	}
	public ArrayList<Users> getUsersNotGroupedList() {
		return usersNotGroupedList;
	}
	public void setUsersNotGroupedList(ArrayList<Users> usersNotGroupedList) {
		this.usersNotGroupedList = usersNotGroupedList;
	}
	
	//methods
	public void list() {
		try {
			this.usersList=usersHome.findAll();
			this.getServletRequest().setAttribute("usersList", usersList);
		} catch (RuntimeException e) {
			addActionError(e.getMessage());
		}
	}

	public void listDisabled() {
		try {
			this.usersListDisabled=usersHome.findDisabled();
			this.getServletRequest().setAttribute("usersListDisabled", usersListDisabled);
		} catch (RuntimeException e) {
			e.printStackTrace();
			addActionError(e.getMessage());
		}
	}

	public void listNotGrouped() {
		try {
			Collection<Users> tempList=usersHome.findAll();
			for (Users u : tempList) {
				if (u.getGroupsList().isEmpty())
					this.usersNotGroupedList.add(u);
			}
			this.getServletRequest().setAttribute("usersNotGroupedList", usersNotGroupedList);
		} catch (RuntimeException e) {
			addActionError(e.getMessage());
		}
	}
}